#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    iniUI();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::iniUI(){
    fLabCurFile = new QLabel;
    fLabCurFile->setMidLineWidth(150);
    fLabCurFile->setText("当前文件：");
    ui->statusBar->addWidget(fLabCurFile);

    progressBar1 = new QProgressBar;
    progressBar1->setMaximumWidth(200);
    progressBar1->setMinimum(5);
    progressBar1->setMaximum(20);
    progressBar1->setValue(ui->textEdit->font().pointSize());
    ui->statusBar->addWidget(progressBar1);

    spinFontSize = new QSpinBox;
    spinFontSize->setMinimum(5);
    spinFontSize->setMaximum(50);
    spinFontSize->setValue(ui->textEdit->font().pointSize());
    spinFontSize->setMinimumWidth(50);

    ui->mainToolBar->addWidget(new QLabel("字体大小"));
    ui->mainToolBar->addWidget(spinFontSize);

    ui->mainToolBar->addSeparator();
    ui->mainToolBar->addWidget(new QLabel("字体"));
    comboFont = new QFontComboBox;
    comboFont->setMinimumWidth(150);
    ui->mainToolBar->addWidget(comboFont);

    setCentralWidget(ui->textEdit);


}

void MainWindow::on_actFontBold_toggled(bool arg1)
{

}

void MainWindow::on_actFontItalic_toggled(bool arg1)
{

}

void MainWindow::on_actFontUnder_toggled(bool arg1)
{

}

void MainWindow::on_textEdit_copyAvailable(bool b)
{
    ui->actCut->setEnabled(b);   // 能否 cut
    ui->actCopy->setEnabled(b);  // 能否copy

    ui->actPaste->setEnabled(ui->textEdit->canPaste());
}

void MainWindow::on_textEdit_selectionChanged()
{

}

void MainWindow::on_spinBoxFontSize_valueChanged(
    int aFontSize) {  //改变字体大小的SpinBox的响应
  QTextCharFormat fmt;
  fmt.setFontPointSize(aFontSize);  //设置字体大小
  ui->textEdit->mergeCurrentCharFormat(fmt);
  progressBar1->setValue(aFontSize);
}

void MainWindow::on_comboFont_currentIndexChanged(
    const QString &arg1) {  // FontCombobox的响应，选择字体名称
  QTextCharFormat fmt;
  fmt.setFontFamily(arg1);  //设置字体名称
  ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MainWindow::iniSignalSlots() {  //信号与槽的关联，当函数带有参数时，必须写明参数的类型
  connect(spinFontSize, SIGNAL(valueChanged(int)), this,
          SLOT(on_spinBoxFontSize_valueChanged(int)));

  connect(comboFont, SIGNAL(currentIndexChanged(const QString &)), this,
          SLOT(on_comboFont_currentIndexChanged(const QString &)));
}
